package com.fjec.devops.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.common.annotation.OpeLog;
import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.view.InfoDetailVO;
import com.fjec.common.view.InfoInformationVO;
import com.fjec.common.view.RpcReturnPage;
import com.fjec.devops.feign.BaseClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class DevopsInformationController {

    @Resource
    private BaseClient baseClient;

    /**
     * 分页查询资讯
     *
     * @param page
     * @param category  类别 1=草莓资讯 2=首页轮播图
     * @param title
     * @param startTime
     * @param endTime
     * @param getTop
     * @return
     */
    @GetMapping("/y/info/get/information/page")
    public JsonResult getInformationPage(Page page,
                                         @RequestParam(required = false) Integer category,
                                         @RequestParam(required = false) String title,
                                         @RequestParam(required = false) String startTime,
                                         @RequestParam(required = false) String endTime,
                                         @RequestParam(required = false) Integer getTop) {
        RpcReturnPage<InfoInformationVO> result = baseClient.rpcGetInformationPage(page.getCurrent(), page.getSize(), category,
                title, startTime, endTime, getTop);
        return JsonResult.success()
                .total(result.getTotal())
                .data(result.getDataList())
                .build();
    }

    /**
     * 保存或修改资讯
     *
     * @param category 类别 1=草莓资讯 2=首页轮播图
     * @param id       资讯Id
     * @param title    标题
     * @param summary  简介
     * @param coverPic 封面url
     * @param articleContent  富文本内容
     * @param display  是否显示 0=不显示 1=显示
     * @param getTop   置顶 0=未置顶 1=已置顶
     * @param space    两边留白
     * @return
     */
    @OpeLog(desc = "保存或修改资讯")
    @PostMapping("/y/info/save/information")
    public JsonResult rpcSaveOrUpdateInformation(@RequestParam(required = false) Integer category,
                                                 @RequestParam(required = false) Long id,
                                                 @RequestParam(required = false) String title,
                                                 @RequestParam(required = false) String summary,
                                                 @RequestParam(required = false) String coverPic,
                                                 @RequestParam String articleContent,
                                                 @RequestParam Integer display,
                                                 @RequestParam(required = false) Integer getTop,
                                                 @RequestParam(required = false) Integer space) {
        if (articleContent.length() > 25000) {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_3031.getCode())
                    .message(EnumSysCode.ERR_3031.getMsg())
                    .build();
        }
        boolean success = baseClient.rpcSaveOrUpdateInformation(category, id, title, summary, coverPic, articleContent, display, getTop, space);
        if (success) {
            if (id == null) {
                return JsonResult.success()
                        .code(EnumSysCode.MSG_6002.getCode())
                        .message(EnumSysCode.MSG_6002.getMsg())
                        .build();
            } else {
                return JsonResult.success()
                        .code(EnumSysCode.MSG_6003.getCode())
                        .message(EnumSysCode.MSG_6003.getMsg())
                        .build();
            }
        } else {
            return JsonResult.failure().build();
        }
    }

    /**
     * 删除资讯
     *
     * @param id
     * @return
     */
    @OpeLog(desc = "删除资讯")
    @PostMapping("/y/info/del/information")
    public JsonResult rpcLogicDelInformation(@RequestParam Long id) {
        boolean success = baseClient.rpcLogicDelInformation(id);
        if (success) {
            return JsonResult.success()
                    .code(EnumSysCode.MSG_6004.getCode())
                    .message(EnumSysCode.MSG_6004.getMsg())
                    .build();
        } else {
            return JsonResult.failure().build();
        }
    }

    /**
     * 根据Id获取资讯
     *
     * @param id
     * @return
     */
    @GetMapping("/y/info/get/by/id")
    public JsonResult rpcGetInfoDetail(Long id) {
        InfoDetailVO view = baseClient.rpcGetInfoDetail(id);
        return JsonResult.success()
                .data(view)
                .build();
    }
}
